// 输入弹框
import {throttle} from "../../../common/debounce_throttle";

const componentBehaviors = require('../../../behaviors/component');
import { COMPONENT_OPTIONS } from '../../../constants/common';

Component({
    behaviors: [componentBehaviors],
    options: {
        ...COMPONENT_OPTIONS,
    },
    /**
     * 组件的属性列表
     */
    properties: {
        title: {
            type: String,
            value: '标题',
        },
        placeholder: {
            type: String,
            value: '',
        },
        show: {
            type: Boolean,
            value: false,
        },
        defaultValue: {
            type: String,
            value: '',
        },
        type: {
            type: String,
            value: 'textarea',
        },
        // 确认按钮文字
        ensureBtnText: {
            type: String,
            value: '确认',
        },
        // 限制长度
        maxlength: {
            type: Number,
            value: -1,
        },
        // 外部控制dialog关闭，confirm时增加dialog控制按钮loading状态
        closeControl: {
            type: Boolean,
            value: false
        }
    },

    observers: {
        'show, defaultValue': function (flag, val) {
            flag && this.setData({
                inputValue: val,
            });
        },
    },

    lifetimes: {
        ready: function() {
            const { closeControl } = this.data
            this.setData({
                beforeClose: closeControl ? (action)  =>  action !== 'confirm'  :  ()  => true
            })
        },
    },

    /**
     * 组件的初始数据
     */
    data: {
        inputValue: '',
        length: 0,
        beforeClose: ()  => true
    },

    /**
     * 组件的方法列表
     */
    methods: {
        // 输入值变化
        onValueChange: function (event) {
            this.setData({
                inputValue: event.detail.value,
                length: event.detail.value.length,
            });
        },
        // 确认
        onConfirm: function () {
            this.triggerEvent('ok', this.data.inputValue)
        },
        // 取消
        onClose: function (e) {
            this.triggerEvent('close', e.detail)
        },
    }
})
